import config from "../../config/index";
const {
    globalData
} = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isEditTitle:{
            type: Boolean,
            value: false, // 图片上传数量
        },
        preview:{
            type: String,
            value: '', // 图片上传数量
        },
        accept:{
            type: String,
            value: 'image', // 图片上传数量
        },
        downloadUrl:{
            type: String,
            value: globalData.imageUrl, // 图片上传数量
        },
        multiple:{
            type: Boolean,
            value: true, // 图片上传数量
        },
        uploadText:{
            type: String,
            value: '', // 图片上传数量
        },
        count: {
            type: Number,
            value: 9, // 图片上传数量
        },
        sizeType: {
            type: Array,
            value: ["original", "compressed"], // 原图 压缩图
        },
        capture: {
            type: Array,
            value: ["album", "camera"], // 从相册选图 使用相机
        },
        files: {
            type: Array,
            value: [],
        }, 
        action:{
            type: String,
            value: 'ZXJC_FILE/fileUploadServlet', // 图片上传数量
        },
        infoId:{
            type: String,
            value: '', // 场景的上级标识
        },
    },
    observers:{
        files:function(e){
            this.setData({
                fileList:e
            })
        }
    },
    attached:function(){
        console.log(this,'filesfiles')
    },

    /**
     * 组件的初始数据
     */
    data: {
       // imagePrefix: globalData.imageUrl,
        fileList: [],
        showIndex:9999,
    },


    /**
     * 组件的方法列表
     */
    methods: {
        chooseScreen(e) {
            console.log("------------infoId"+this.data.infoId)
            const index= e.currentTarget.dataset
            this.setData({
                showIndex:9999
            })
            console.log(index,e)
            if(this.data.isEditTitle){
                this.triggerEvent("editTile",index)
            }
        },
        editScreen(e) {
            const id = this.data.infoId;
            const type = "3";
            wx.navigateTo({
            url: "/vr/pages/vr-preview/vr-preview?infoId=" + id+"&findVrType="+type,
            });
        },
        showScreen(e) {
            const id = this.data.infoId;
            const type = "3";
            wx.navigateTo({
            url: "/vr/pages/vr-info/vr-info?infoId=" + id+"&findVrType="+type,
            });
        },
        editTile(e){
           const index= e.currentTarget.dataset.index
           this.setData({
            showIndex:index
           })
        },
        previewImage(e){
            const {fileList,imagePrefix,downloadUrl}=this.data
            const {index}= e.currentTarget.dataset
            console.log(downloadUrl+fileList[index].url)
            wx.previewImage({
                current:downloadUrl+fileList[index].url,
                urls: fileList.map(item=>(downloadUrl+item.url)),
            })
            
        },
        //文件上传
        async successUpload() {
            let _this=this;
            console.log("------------successUpload")
            wx.chooseMedia({
                count:_this.data.count,
                //type:_this.data.accept,
                mediaType:['image'],
              success:async(event)=>{
                  wx.showLoading({
                    title: '上传中....',
                    mask:true
                  })
                const files = event.tempFiles;
                console.log(files)
                const urls =await Promise.all(files.map(item => _this.upload(item)))
                const fileList=[..._this.data.fileList,...urls]
                _this.setData({
                    fileList
                })
                wx.hideLoading()
                _this.triggerEvent("changefiles",fileList)
              }
            })

        },
        //上传
        upload(file) {
            return new Promise((resolve, reject) => {
                wx.uploadFile({
                    url: `${config.uploadUrl}/${this.data.action}`,
                    filePath: file.tempFilePath,
                    name: "file",
                    success: (r) => {
                        // console.log(typeof r.data);
                        const data = JSON.parse(r.data);
                        if (data.success) {
                            const result = data.result[0];
                            resolve({...file,url:result,deletable: true,name:result})
                        }
                    },
                    fail: (e) => {
                        wx.showToast({
                            title: '上传失败',
                            icon: 'none'
                        })
                        throw e
                    }
                });
            })
        },
        //删除
        deleteImage(e){
        const {index}= e.currentTarget.dataset
        this.data.fileList.splice(index,1);
        this.triggerEvent("changefiles",this.data.fileList)
        this.setData({
            fileList:this.data.fileList
        })
        }
    }
})